Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.IO
Imports System.IO.Compression
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports ViewStateEliminator_VB.ViewStateEliminator.Configuration
Imports ViewStateEliminator_VB.ViewStateEliminator.Data
Imports System.Configuration
Namespace ViewStateEliminator.UI

    Public Class BaseNoViewStatePage
        Inherits System.Web.UI.Page
        Private _viewstateStorage As IViewstateStorage
        Private _config As ViewStateConfiguration

        Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
    
            Dim retVal As Object
            Dim id As String = Request.Form("__VIEWSTATE")
            Dim bytes() As Byte = Nothing
            _config = CType(ConfigurationManager.GetSection("ViewStateConfiguration"), ViewStateConfiguration)
            _viewstateStorage = ViewstateStorageFactory.GetStorageMedium()
            bytes = Convert.FromBase64String(_viewstateStorage.Get(id).Value)
            If (_config.UseCompression) Then
                bytes = Util.Decompress(bytes)
            End If

            Dim formatter As LosFormatter = New LosFormatter()
            retVal = formatter.Deserialize(Convert.ToBase64String(bytes))
            Return retVal

        End Function
        Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As Object)
            _viewstateStorage = ViewstateStorageFactory.GetStorageMedium()
            _config = CType(ConfigurationManager.GetSection("ViewStateConfiguration"), ViewStateConfiguration)
            Dim formatter As LosFormatter = New LosFormatter()
            Dim writer As StringWriter = New StringWriter()
            formatter.Serialize(writer, ViewState)
            Dim viewStateString As String = writer.ToString()
            Dim bytes() As Byte = Convert.FromBase64String(viewStateString)
            If (_config.UseCompression) Then
                bytes = Util.Compress(bytes)
            End If
            Dim viewstateBase64 As String = Convert.ToBase64String(bytes)
            Dim id As String = ViewstateStorageFactory.GetUserUniqueID().GetUniqueID()
            _viewstateStorage.Add(New KeyValuePair(Of Object, String)(id, viewstateBase64))
            ClientScript.RegisterHiddenField("__VIEWSTATE", id)
        End Sub
    End Class

    Public Module Util
        Public Function Decompress(ByVal data As Byte()) As Byte()
            Dim input As MemoryStream = New MemoryStream()
            input.Write(data, 0, data.Length)
            input.Position = 0
            Dim gzip As GZipStream = New GZipStream(input, CompressionMode.Decompress, True)
            Dim output As MemoryStream = New MemoryStream()
            Dim buff(64) As Byte
            Dim read As Integer = -1
            read = gzip.Read(buff, 0, buff.Length)
            While read > 0
                output.Write(buff, 0, read)
                read = gzip.Read(buff, 0, buff.Length)
            End While
            gzip.Close()
            Return output.ToArray()
        End Function
        Public Function Compress(ByVal data As Byte()) As Byte()
            Dim output As MemoryStream = New MemoryStream()
            Dim gzip As GZipStream = New GZipStream(output, CompressionMode.Compress, True)
            gzip.Write(data, 0, data.Length)
            gzip.Close()
            Return output.ToArray()
        End Function
    End Module
End Namespace
